package com.bc004346.travelus.Model.Abstract;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MenuItem;

import com.bc004346.travelus.Fragment.FlightListFragment;
import com.bc004346.travelus.R;
import com.bc004346.travelus.Utility.Utility;

import java.text.ParseException;

/**
 * Created by Renats on 9/26/2014.
 * Abstract class for any activity that will display a list of flights
 */
public abstract class FlightListActivity extends SingleFragmentActivity {

    protected FlightListFragment mFragment = new FlightListFragment();

    @Override
    protected Fragment getFragment() {
        return mFragment;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        try {
            mNavigationDrawer.handleOnOptionsItemSelected(item);

            switch (item.getItemId()) {
                case R.id.menuRefresh:
                    removeTaskFragment();
                    refresh();
                    break;
            }
        } catch (Exception e) {
            Utility.log(e.toString());
            mStatus.setText(e.getMessage());
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            Utility.onStart(this);
            setTitle("My Flights");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    protected void removeTaskFragment() {
        try {
            android.app.Fragment fragment =
                    getFragmentManager().findFragmentByTag(getTaskTag());

            toggleMenuState(true);
            if (fragment != null)
                getFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();

        } catch (Exception e) {
            Utility.log(e.toString());
            mStatus.setText(e.getMessage());
        }
    }

    protected abstract void refresh();

    protected abstract String getTaskTag();
}
